/** 升级面板 */
cc.Class({
    extends: cc.Component,

    properties: {
        /** 建筑图标 */
        sp_pic:cc.Sprite,
        /** 建筑说明 */
        txt_demo:cc.Label,
        /** 升级按钮 */
        bt_up:cc.Button,
        /** 拆除按钮 */
        bt_remove:cc.Button,
        /** 操作位置id */
        _aid:cc.Integer,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        /** 升级 */
        this.bt_up.node.on(cc.Node.EventType.TOUCH_END, function(ev){
            let bid = this._aid;
            if(this._aid > 11) bid = App.U.city[App.cid].area[this._aid-1].bid;
            let cb = true, ct = App.U.city[App.cid], bd = App.C.build[bid].list[ct.area[this._aid-1].lev], tip = "名称：<b><color=#C5C21F>" + App.C.build[bid].name + "</color></b>";
            if(bd[0].length > 0){
                tip += "<br/>前提："
                for(let i=0;i<bd[0].length;i+=2){
                    if(bd[0][i] < 12){
                        if(ct.area[bd[0][i]-1] && ct.area[bd[0][i]-1].lev >= bd[0][i+1]){
                            tip += " " + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name;
                        }else{
                            tip += " <color=#FF0000>" + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name + "</color>";
                            cb = false;
                        }
                    }else{
                        let fd = false;
                        for(let j=11;j<ct.area.length;j++){
                            if(ct.area[j].bid == bd[0][i] && ct.area[j].lev >= bd[0][i+1]){
                                fd = true;
                                break;
                            }
                        }
                        if(fd){
                            tip += " " + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name;
                        }else{
                            tip += " <color=#FF0000>" + bd[0][i+1] + "级" + App.C.build[bd[0][i]].name + "</color>";
                            cb = false;
                        }
                    }
                }
            }
            let re = ["coin","wood","ore","sulfur","mercury","crystal","stone"], ren = ["金币","木头","矿石","硫磺","水银","水晶","宝石"];
            tip += "<br/><br/>[<color=#dcc86a>升级所需资源</color>]";
            for(let n=0;n<re.length;n++){
                if(bd[5+n] > App.U.res[re[n]]){
                    tip += "<br/>"+ren[n]+":<color=#FF0000>"+bd[5+n]+"</color>";
                    cb = false;
                }else{
                    tip += "<br/>"+ren[n]+":"+bd[5+n];
                }
            }
            tip += "<br/><br/>耗时：" + bd[4] + "秒";
            if(cb == false) return App.showtip(tip);
            App.net.send([2,14,{aid:this._aid,cid:App.cid}]);
        }, this);
        /** 拆除 */
        this.bt_remove.node.on(cc.Node.EventType.TOUCH_END, function(ev){
            //cc.log("bt_remove");
            if(this._aid > 11){
                App.net.send([2,17,{aid:this._aid,cid:App.cid}]);
            }
        }, this);
    },

    start () {

    },

    /** 显示信息 */
    showInfo(aid){
        this._aid = aid;
        if(aid < 12){
            //this.bt_remove.node.active = false;
            this.bt_remove.interactable = false;
            cc.log("updatearea", aid, this.bt_remove.interactable);
        }
        let bd = App.U.city[App.cid].area[aid-1], bi = App.C.build[bd.bid];
        this.sp_pic.spriteFrame = new cc.SpriteFrame(App.path + "race/r" + App.U.city[App.cid].race + "_" + bi.list[bd.lev-1][3] + ".png");
        this.txt_demo.string = bi.desc.replace(/\{0\}/g, bd.lev+"级").replace(/\{2\}/g, bi.list[bd.lev-1][2]);
        if(bd.lev >= bi.list.length) this.bt_up.interactable = false;
    }

    // update (dt) {},
});
